<?

/**
 * Package collector. 
 * Save info about each entity from spotlight.xml configuration file.
 *   
 * @package SpotLight.core.configurator.collector
 * @version since begin
 * @author asmhack
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 * @todo rewite all collectors with Iterator extends.
 */
class PackageColl {
	
	private $name;
	
	private $dispatchers = array ();
	
	private $actions = array ();
	
	private $interceptors = array ();
	
	private $flows = array ();
	
	private $template = '';
	
	private $excludes = array ();
	
	private $tagLibs = array ();
	
	public function __construct() {
	
	}
	
	public function addTagLib($prefix, $tagLibColl) {
		$this->tagLibs [$prefix] = $tagLibColl;
	}
	
	public function getTagLibs() {
		return $this->tagLibs;
	}
	
	public function getTagLib($prefix) {
		if(key_exists($prefix,$this->tagLibs))
			return $this->tagLibs[$prefix];
		return null;
	}
	
	public function setName($name) {
		$this->name = $name;
	}
	
	public function setTemplate($template) {
		$this->template = $template;
	}
	
	public function getTemplate() {
		return $this->template;
	}
	
	public function addAction($action) {
		$this->actions [] = $action;
	}
	
	public function addFlow($name, $flow) {
		$this->flows [$name] = $flow;
	}
	
	public function hasFlows() {
		return count ( $this->flows ) > 0;
	}
	
	public function addDispatcher($type, $dispatcher) {
		$this->dispatchers [$type . ''] = $dispatcher;
	}
	
	public function getDispatcher($type) {
		return $this->dispatchers [$type];
	}
	
	public function addInterceptor($filter) {
		$this->interceptors [] = $filter;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function getInterceptors() {
		return $this->interceptors;
	}
	
	public function addExclude($name) {
		$this->excludes [$name] = 1;
	}
	
	public function isInExcludes($name, $flowName = null) {
		if ($flowName == null)
			return array_key_exists ( $name, $this->excludes );
		else
			return array_key_exists ( $name, $this->excludes ) || $this->flows [$flowName]->isInExcludes ( $name );
	}
	
	public function getInterceptorsForExecute($type, $flowName = null, $actionName) {
		$ret = array ();
		
		foreach ( $this->interceptors as $interceptor ) {
			if ($interceptor->checkUrl ( $actionName ) && $interceptor->isType ( $type )) {
				$ret [] = $interceptor;
			}
		}
		
		if ($flowName !== null) {
			$flowRet = $this->flows [$flowName]->getInterceptorsForExecute ( $type, $actionName );
			if (is_array ( $flowRet ))
				foreach ( $flowRet as $flowInterceptor )
					$ret [] = $flowInterceptor;
		}
		
		return $ret;
	}
	
	public function getActions() {
		return $this->actions;
	}
	
	/**
	 * return action config object or null;
	 *
	 * @param string $actionName
	 * @return core.collector.Action
	 */
	public function getAction($actionName) {
		#all actions test
		foreach ( $this->actions as $act ) {
			if (preg_match ( '/^' . str_replace ( '/', '\/', $act->getName () ) . '/', $actionName, $matches ))
				return array (0 => $act, 1 => $matches, 2 => false ); // 0=> ActionColl object; 1=> array of action's name matches; 2=>is flow's action
		}
		#all flow's actions test
		

		foreach ( $this->flows as $flow ) {
			$action = $flow->getAction ( $actionName );
			if ($action !== null)
				return $action;
		}
		return null;
	}
	
	public function __toString() {
		$txt = 'Package <b style="color:blue">' . $this->name . '</b> {<br/>';
		foreach ( $this->interceptors as $filter ) {
			$txt .= '&nbsp;&nbsp;&nbsp;' . $filter . '<br/>';
		}
		$txt .= '<br/>';
		foreach ( $this->actions as $act ) {
			$txt .= '&nbsp;&nbsp;&nbsp;' . $act . '<br/>';
		}
		$txt .= '<br/>';
		foreach ( $this->flows as $flow ) {
			$txt .= '&nbsp;&nbsp;&nbsp; ' . $flow . '<br/>';
		}
		$txt .= '&nbsp;&nbsp;}';
		return $txt;
	}
}
?>